﻿<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini">
            <div class="layui-form toolbar" id="tbToolBar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">账&emsp;号：</label>
                        <div class="layui-input-inline mr0">
                            <input name="username" class="layui-input" type="text" placeholder="输入账号"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">用户名：</label>
                        <div class="layui-input-inline mr0">
                            <input name="name" class="layui-input" type="text" placeholder="输入用户名"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">性&emsp;别：</label>
                        <div class="layui-input-inline mr0">
                            <select name="sex">
                                <option value="">选择性别</option>
                                <option value="male">男</option>
                                <option value="female">女</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline" style="padding-right: 110px;">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchUser" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnAddUser" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="tableUser" lay-filter="tableUser"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarUser">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="reset">重置密码</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="tableStateUser">
    <input type="checkbox" lay-filter="ckStateUser" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定" {{d.status==true?'checked':''}}/>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelUser">
    <form id="modelUserForm" lay-filter="modelUserForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">账号</label>
            <div class="layui-input-block">
                <input name="username" placeholder="请输入账号" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入用户名" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input name="phone" placeholder="请输入手机号" type="text" class="layui-input" maxlength="20"
                lay-verType="tips" lay-verify="phone" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">身份证号</label>
            <div class="layui-input-block">
                <input name="idcard" placeholder="请输入身份证号" type="text" class="layui-input" maxlength="20"
                lay-verType="tips" lay-verify="identity" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="male" title="男" checked/>
                <input type="radio" name="sex" value="female" title="女"/>
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block" id="selectRoleBox">
                    <select name="rolename" lay-verType="tips" lay-verify="required">
                        <option value="">请选择角色</option>
                        {{# d.forEach(function(e,i){ }}
                        <option value="{{ e.rolename }}">{{ e.name }}</option>  
                        {{# }) }}
                    </select>
            </div>
        </div>
        
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="modelSubmitUser" lay-submit>保存</button>
        </div>
    </form>
</script>

<script type="text/html" id="selectRole">

</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'config'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var config = layui.config;
        var laytpl = layui.laytpl;
        var modelUserHtml = ""
        var initRole = function(){
            //初始化角色select
            admin.req("/api/sys/role?limit=100",{},function(res){
                if(res.status==200){
                    // console.log($("#modelUser").html())
                    laytpl($("#modelUser").html()).render(res.data,function(html){
                        modelUserHtml = html                       
                    })
                }else{
                    laytpl($("#modelUser").html()).render([{}],function(html){
                        modelUserHtml = html
                    })
                }
            },"get")
        }
        initRole()

        form.render('select');
        // 渲染表格
        var insTb = table.render({
            elem: '#tableUser',
            url: config.base_server + '/api/sys/user',
            page: true,
            toolbar: true,
            cellMinWidth: 100,
            headers:{"Authorization":config.getToken()},
            cols: [[

                {type: 'numbers', title: '#'},
                {field: 'username', sort: true, title: '账号'},
                {field: 'name', sort: true, title: '用户名'},
                {field: 'sex', sort: true, title: '性别', templet:function(d){
                        if(d.sex=="male"){return "男"}
                        if(d.sex=="female"){return "女"}
                        return d.sex
                    }
                },
                {field:'createTime', sort: true, templet: function (d) {
                    if(d.createtime){
                        var timeobj=new Date(Date.parse(d.createtime.split("+")[0].replace("T"," ").replace(/-/g, "/")));
                        return util.toDateString(timeobj,"yyyy-MM-dd HH:mm:ss");}
                    else{
                            return ""
                    }
                    }, title: '创建时间'
                },
                {field: 'status', sort: true, templet: '#tableStateUser', title: '状态'},
                {align: 'center', toolbar: '#tableBarUser', title: '操作', minWidth: 200}
            ]]
        });

        // 添加
        $('#btnAddUser').click(function () {
            showEditModel();
        });

        // 搜索
        form.on('submit(formSubSearchUser)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });

        // 工具条点击事件
        table.on('tool(tableUser)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(data.id, data.name);
            } else if (layEvent === 'reset') { // 重置密码
                resetPsw(data.id, data.name);
            }
        });

        // 显示表单弹窗
        function showEditModel(mUser) {
            var method = mUser? "put":"post"
            var url = mUser?"/api/sys/user/"+mUser.id :"/api/sys/user"
            admin.open({
                type: 1,
                title: (mUser ? '修改' : '添加') + '用户',
                content: modelUserHtml,
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    //var url = "/api/sys/user"
                    // 回显数据
                    // console.log(mUser)
                    form.val('modelUserForm', mUser);

                    // 表单提交事件
                    form.on('submit(modelSubmitUser)', function (data) {
                        layer.load(2);
                        admin.req(url, data.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({}, 'data');
                            } else {
                                // console.log(res)
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, method);
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDel(userId, nickName) {
            layer.confirm('确定要删除“' + nickName + '”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('/api/sys/user/'+userId,{}, function (res,_,v) {
                    layer.closeAll('loading');
                    var status = undefined
                    if(!v){
                        status = res.status
                    }else{
                        status = v.status
                    }
                    if (status == 204) {
                        layer.msg("删除成功", {icon: 1});
                        insTb.reload({}, 'data');
                    } else {
                        layer.msg("删除失败", {icon: 2});
                    }
                }, 'delete');
            });
        }

        // 修改用户状态
        form.on('switch(ckStateUser)', function (obj) {
            layer.load(2);
            admin.req('/api/sys/user/'+obj.elem.value, {
                status: obj.elem.checked ? true : false
            }, function (res,_,v) {
                layer.closeAll('loading');
                if (v.status == 200) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'put');
        });

        // 重置密码
        function resetPsw(userId, nickName) {
            layer.confirm('确定要重置“' + nickName + '”的登录密码吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('/api/sys/user/resetpsw/'+userId, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'post');
            });
        }

    });
</script>
